<?php

namespace App\Http\Repository;

use App\Chunk;
use App\Info;
use App\InfoImageLines;
use App\InfoSortLines;
use App\Sort;

class InfoRepository
{
    private $info;
    private $chunk;
    private $sort;
    private $infosortlines;
    private $infoimagelines;

    public function __construct(Info $info, InfoSortLines $infoSortLines, InfoImageLines $infoImageLines, Sort $sort, Chunk $chunk)
    {
        $this->info = $info;
        $this->infosortlines = $infoSortLines;
        $this->infoimagelines = $infoImageLines;
        $this->sort = $sort;
        $this->chunk = $chunk;
    }

    //得到模块信息
    public function getInfo($chunk, $request)
    {
        $infos = $this->info
            ->select('infos.*', \DB::raw('group_concat(sorts.name) as sort_name'), 'images.path as image', 'images.water')
            ->join('info_sort_lines', 'info_sort_lines.info', '=', 'infos.id')
            ->join('sorts', 'info_sort_lines.sort', '=', 'sorts.id')
            ->leftjoin('info_image_lines', 'info_image_lines.info', '=', 'infos.id')
            ->leftjoin('images', 'images.id', '=', 'info_image_lines.image')
            ->orderBY('infos.id', 'DESC')
            ->groupBy('infos.id')
            ->where('info_sort_lines.chunk', $chunk)
            ->where(function ($query) use ($request) {
                if ($request->has('sort')) {
                    $query->where('sorts.id', $request->sort)->Orwhere('sorts.path', 'like', '%,' . $request->sort . ',%');
                }
            })
            ->where(function ($query) use ($request) {
                if ($request->has('is')) {
                    switch ($request->is) {
                        case 'is_hot':
                            $query->where('infos.is_hot', 1);
                            break;
                        case 'is_new':
                            $query->where('infos.is_new', 1);
                            break;
                        case 'is_push':
                            $query->where('infos.is_push', 1);
                            break;
                    }
                }
            })
            ->where(function ($query) use ($request) {
                if ($request->has('search')) {
                    $query->where('infos.name', 'like', '%' . $request->search . '%')
                        ->orWhere('infos.id', $request->search);
                }
            })
            ->paginate(config('app.info_page_num'));
        foreach ($infos as $k => $info) {
            $infos[$k]->image = !empty($info->water) ? $info->water : $info->image;
            $infos[$k]->sort_name = implode(',', array_unique(explode(',', $info->sort_name)));
        }

        return $infos;
    }

    //得到模块信息
    public function getInfoOnlyTrashed($chunk, $request)
    {
        return $this->info
            ->onlyTrashed()
            ->select('infos.*', \DB::raw('group_concat(sorts.name) as sort_name'))
            ->join('info_sort_lines', 'info_sort_lines.info', '=', 'infos.id')
            ->join('sorts', 'info_sort_lines.sort', '=', 'sorts.id')
            ->orderBY('infos.deleted_at', 'DESC')
            ->groupBy('infos.id')
            ->where('info_sort_lines.chunk', $chunk)
            ->where(function ($query) use ($request) {
                if ($request->has('sort')) {
                    $query->where('sorts.id', $request->sort)->Orwhere('sorts.path', 'like', '%,' . $request->sort . ',%');
                }
            })
            ->where(function ($query) use ($request) {
                if ($request->has('is')) {
                    switch ($request->is) {
                        case 'is_hot':
                            $query->where('infos.is_hot', 1);
                            break;
                        case 'is_new':
                            $query->where('infos.is_new', 1);
                            break;
                        case 'is_push':
                            $query->where('infos.is_push', 1);
                            break;
                    }
                }
            })
            ->where(function ($query) use ($request) {
                if ($request->has('search')) {
                    $query->where('infos.name', 'like', '%' . $request->search . '%');
                }
            })
            ->paginate(config('app.info_page_num'));
    }

    //得到信息 包含图片 包含分类
    public function getInfoWithImageAndSort($id)
    {
        $info = $this->info
            ->findorfail($id);
        $info['images'] = $this->infoimagelines
            ->join('images', 'images.id', '=', 'info_image_lines.image')
            ->where('info_image_lines.info', $id)
            ->lists('images.path', 'images.id');
        $info['sorts'] = $this->infosortlines
            ->where('info_sort_lines.info', $id)
            ->lists('sort')
            ->toArray();
        return $info;
    }

    //按照模块分类 得到信息列表
    public function getInfoGroupChunk()
    {
        $chunks = $this->chunk
            ->where('status', 1)
            ->lists('path', 'id');
        $infos = [];
        foreach ($chunks as $id => $chunk) {
            $infos[$chunk] = $this->info
                ->join('info_sort_lines', 'info_sort_lines.info', '=', 'infos.id')
                ->join('sorts', 'sorts.id', '=', 'info_sort_lines.sort')
                ->join('chunks', 'chunks.id', '=', 'info_sort_lines.chunk')
                ->leftjoin('info_image_lines', 'info_image_lines.info', '=', 'infos.id')
                ->leftjoin('images', 'images.id', '=', 'info_image_lines.image')
                ->where('info_sort_lines.chunk', $id)
                ->whereNull('set_time_out')
                ->where('infos.status', 1)
                ->where('sorts.status', 1)
                ->orderBy('xindex', 'DESC')
                ->orderBy('created_at', 'DESC')
                ->groupBy('infos.id')
                ->select('infos.*', 'infos.created_at as time', 'chunks.path as chunk', 'images.path as image', 'images.water', 'sorts.name as sort_name', 'sorts.id as sort_id')
                ->get()
                ->toArray();
            foreach ($infos[$chunk] as $k => $info) {
                $infos[$chunk][$k]['image'] = !empty($info['water']) ? $info['water'] : $info['image'];
                if (!empty($info['file'])) {
                    $infos[$chunk][$k]['filesize'] = calc(filesize('.' . $info['file']));
                }
            }
        }

        return $infos;
    }

    //得到用户当前发布的信息的数量 按照模块分类
    public function getInfoNumByUserGroupChunk($userId)
    {
        $tmpInfos = $this->info
            ->where('chunks.status', 1)
            ->where('chunks.user_write', 1)
            ->where('sorts.status', 1)
            ->where('sorts.user_write', 1)
            ->where('infos.author_role', 2)
            ->where('infos.author', $userId)
            ->join('info_sort_lines', 'info_sort_lines.info', '=', 'infos.id')
            ->join('chunks', 'chunks.id', '=', 'info_sort_lines.chunk')
            ->join('sorts', 'sorts.id', '=', 'info_sort_lines.sort')
            ->groupBy('infos.id')
            ->lists('chunks.id as chunk', 'infos.id')
            ->toArray();

        return array_count_values($tmpInfos);
    }

    //根据用户id得到模块信息
    public function getInfoByUserId($chunk, $request, $userId)
    {
        $infos = $this->info
            ->select('infos.*', \DB::raw('group_concat(sorts.name) as sort_name'), 'images.path as image', 'images.water')
            ->join('info_sort_lines', 'info_sort_lines.info', '=', 'infos.id')
            ->join('sorts', 'info_sort_lines.sort', '=', 'sorts.id')
            ->leftjoin('info_image_lines', 'info_image_lines.info', '=', 'infos.id')
            ->leftjoin('images', 'images.id', '=', 'info_image_lines.image')
            ->orderBY('infos.id', 'DESC')
            ->groupBy('infos.id')
            ->where('sorts.user_write', 1)
            ->where('infos.author_role', 2)
            ->where('infos.author', $userId)
            ->where('info_sort_lines.chunk', $chunk)
            ->where(function ($query) use ($request) {
                if ($request->has('sort')) {
                    $query->where('sorts.id', $request->sort)->Orwhere('sorts.path', 'like', '%,' . $request->sort . ',%');
                }
            })
            ->where(function ($query) use ($request) {
                if ($request->has('is')) {
                    switch ($request->is) {
                        case 'is_hot':
                            $query->where('infos.is_hot', 1);
                            break;
                        case 'is_new':
                            $query->where('infos.is_new', 1);
                            break;
                        case 'is_push':
                            $query->where('infos.is_push', 1);
                            break;
                    }
                }
            })
            ->where(function ($query) use ($request) {
                if ($request->has('search')) {
                    $query->where('infos.name', 'like', '%' . $request->search . '%');
                }
            })
            ->paginate(10);
        foreach ($infos as $k => $info) {
            $infos[$k]->image = !empty($info->water) ? $info->water : $info->image;
            $infos[$k]->sort_name = implode(',', array_unique(explode(',', $info->sort_name)));
        }

        return $infos;
    }
}